﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace TeachingEval.Sys.Common
{
    //public delegate void WriteLogDel(string str);
    public class LogHelper
    {
        public static Queue<string> ExceptionStringQueue = new Queue<string>();
        //public static WriteLogDel WriteLogDelFunc;
        static LogHelper()
        {
            //WriteLogDelFunc = new WriteLogDel(WriteLogToFile);
            //WriteLogDelFunc += WriteLogToMongodb;
            
            //把从队列中获取错误信息写到日志文件里面去。
            ThreadPool.QueueUserWorkItem(o =>
                {
                    lock (ExceptionStringQueue)
                    {
                        string str = ExceptionStringQueue.Dequeue();
                        //把异常文件写到日志文件里面去
                        //变化点：有可能写到日志文件，有可能写到数据库里面去，有可能两个地方都写
                        
                        //执行委托方法。把异常信息写到委托里面去。 
                        //WriteLogDelFunc(str);
                    }
                });
        }

//         public static void WriteLogToFile(string txt) { }
// 
//         public static void WriteLogToMongodb(string txt) { }

        public static void WriteLog(string excetionText)
        {
            lock (ExceptionStringQueue)
            {
                ExceptionStringQueue.Enqueue(excetionText);
            }
        }
    }
}
